
using Microsoft.EntityFrameworkCore;
using Rbac.Api.Data;
using Rbac.Api.ServiceInterface;

namespace Rbac.Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.


            var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

            builder.Services.AddDbContext<UserDbContext>(options =>
                options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            // 自动扫描注册
            builder.Services.Scan(scan =>
                scan.FromAssemblyOf<IScopedService>() // 扫描包含IScopedService接口的程序集
                                                      // 所有实现了IScopedService接口的类
                    .AddClasses(classes => classes.AssignableTo<IScopedService>())
                    .AsImplementedInterfaces() // 注册为其实现的接口
                    .WithScopedLifetime() // 设置生命周期为Scoped
                );


            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
